using System;
using System.Collections.Generic;
using System.Text;
using BizTalkCop.Rules.Bases;
using Microsoft.FxCop.Sdk;
using Microsoft.XLANGs.BaseTypes;

namespace BizTalkCop.Rules.Naming.Rules
{
    /// <summary>
    /// Rule verifying that schemas are named with their schema format
    /// </summary>
    public class SchemaNamesShouldIndicateFormat : ConfiguredRuleBase
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public SchemaNamesShouldIndicateFormat() : base("SchemaNamesShouldIndicateFormat") { }

        /// <summary>
        /// Checks if the schema name ends with the standard
        /// </summary>
        /// <param name="type">Type to check</param>
        /// <returns>Problem collection</returns>
        public override ProblemCollection Check(TypeNode type)
        {
            if (IsDocumentSchema(type))
            {
                string separator = this.GetConfiguredSetting(Setting.Separator);
                string formats = this.GetConfiguredSetting(Setting.Formats);
                List<string> knownFormats = new List<string>(formats.Split(';'));
                string[] nameParts = type.Name.Name.Split(separator.ToCharArray());
                //check the final part:
                if (!knownFormats.Contains(nameParts[nameParts.Length - 1]))
                {
                    Problems.Add(new Problem(GetNamedResolution("Standard", nameParts[nameParts.Length - 1]), type));
                }
            }
            return Problems;
        }

        #region Configured settings

        static SchemaNamesShouldIndicateFormat()
        {
            Dictionary<string, string> defaults = new Dictionary<string, string>();
            defaults.Add(Setting.Separator, Default.Separator);
            defaults.Add(Setting.Formats, Default.Formats);
            SetDefaults(typeof(SchemaNamesShouldIndicateFormat), defaults);
        }

        private struct Setting
        {
            public const string Separator = "Separator";
            public const string Formats = "Formats";
        }

        private struct Default
        {
            public const string Separator = "_";
            public const string Formats = "FLAT;SQL;SOAP;XML";
        }

        #endregion
    }
}

